package com.zjw.contorller;

import cn.hutool.crypto.SecureUtil;
import com.wf.captcha.SpecCaptcha;


import com.wf.captcha.base.Captcha;
import com.wf.captcha.utils.CaptchaUtil;
import com.zjw.domain.vo.CaptchaVo;
import com.zjw.domain.vo.ResponseResult;
import com.zjw.system.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class CaptchaController {

    @Autowired
    private StringRedisTemplate redisTemplate;


    @RequestMapping("/captcha")
    public ResponseResult<CaptchaVo> captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {

        SpecCaptcha specCaptcha = new SpecCaptcha(
                SystemConstant.CAPTCHA_IMG_WIDTH,
                SystemConstant.CAPTCHA_IMG_HEIGHT,
                SystemConstant.CAPTCHA_IMG_LEN);
        specCaptcha.setCharType(Captcha.TYPE_DEFAULT);



        String verCode = specCaptcha.text().toLowerCase();
        String key = UUID.randomUUID().toString();
//        Redis中存储
        redisTemplate.opsForValue().set(key, verCode, SystemConstant.CAPTCHA_VALID_TIME_MINUTE, TimeUnit.MINUTES);

        CaptchaVo res = new CaptchaVo(key, specCaptcha.toBase64());
        return ResponseResult.ok(res);
    }
}
